<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/12/21
 * Time: 15:57
 */

namespace app\crm\controller;


use app\crm\model\GameModel;
use app\crm\model\GameTagModel;
use app\crm\model\GameTypeModel;
use app\crm\traits\Upload;
use think\Request;
use think\Validate;

class Game extends BaseController
{

    use Upload;

    public function gameList(Request $request)
    {
        //游戏标签
        $tagList = GameTagModel::all([]);

        //游戏分类
        $typeList = GameTypeModel::all();

        $pageSize = $request->get('pageSize', 10);
        $where = [];

        $lists = GameModel::pagination($where, $pageSize);

        $page = $lists->render();
        $lists = $lists->toArray();
        foreach ($lists['data'] as $k => $v) {
            if ($v['tag']) {
                $tagTmp = explode(',', $v['tag']);
                $tag = '';
                foreach ($tagTmp as $v2) {
                    foreach ($tagList as $v3) {
                        if ($v2 == $v3['id']) {
                            $tag .= $v3['name'] . '|';
                        }
                    }
                }
                $lists['data'][$k]['tag'] = $tag;
            }
            foreach ($typeList as $v2) {
                if ($v['type'] == $v2['id']) {
                    $lists['data'][$k]['type'] = $v2['name'];
                }
            }
        }

        $this->assign('page', $page);
        $this->assign('list', $lists['data']);
        return $this->fetch('game');
    }

    /**
     * 游戏添加界面
     * @return mixed
     */
    public function gameAdd()
    {
        //游戏分类
        $typeList = GameTypeModel::all();
        $this->assign('type', $typeList);
        //游戏标签
        $tagList = GameTagModel::all([]);
        $this->assign('tag', $tagList);
        return $this->fetch('game-add');
    }

    /**
     * 游戏添加界面
     * @param Request $request
     * @return \think\response\Json
     */
    public function gameAddAct(Request $request)
    {
        $data = [];
        $data['name'] = $request->post('name', '');
        $data['gid'] = $request->post('gid', '');
        $data['major_type'] = $request->post('major_type', '');
        $data['type'] = $request->post('type', '');
        $data['des'] = $request->post('des', '');
        $data['img'] = $request->post('img', '');
        $data['order_id'] = $request->post('order_id', 0);
        $data['tag'] = '';
        $tag = isset($_POST['tag']) ? $_POST['tag'] : '';
        if (is_array($tag)) {
            $data['tag'] = implode(',', $tag);
        }
        $validate = new Validate([
            'name' => 'require',
            'gid' => 'require',
            'major_type' => 'require',
            'type' => 'require',
            'des' => 'require',
        ]);
        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        $where = [];
        $where['name'] = $data['name'];
        $isExist = GameModel::one($where);
        if ($isExist) {
            return json(['code' => 0, 'msg' => '游戏名已被占用']);
        }
        $data['create_at'] = time();
        $res = GameModel::insert($data);
        $output = $res ? ['code' => 1, 'msg' => '添加成功'] : ['code' => 0, 'msg' => '添加失败,请重试'];
        return json($output);
    }



    public function edit(Request $request)
    {
        $gameId = $request->get('id', '');
        $info = GameModel::one(['id' => $gameId]);
        $info['tag'] = explode(',', $info['tag']);
        if ($info) {
            $this->assign('info', $info);
        }
        //游戏分类
        $typeList = GameTypeModel::all();
        $this->assign('type', $typeList);
        //游戏标签
        $tagList = GameTagModel::all([]);
        $this->assign('id', $gameId);
        $this->assign('tag', $tagList);
        return $this->fetch('game-edit');
    }

    public function editAct(Request $request)
    {
        $id = $request->post('id', '');
        if (!is_numeric($id)) {
            return json(['code' => 0, 'msg' => 'id格式不正确']);
        }
        $data = [];
        $data['name'] = $request->post('name', '');
        $data['gid'] = $request->post('gid', '');
        $data['major_type'] = $request->post('major_type', '');
        $data['type'] = $request->post('type', '');
        $data['des'] = $request->post('des', '');
        $data['img'] = $request->post('img', '');
        $data['order_id'] = $request->post('order_id', 0);
        $data['tag'] = '';
        $tag = isset($_POST['tag']) ? $_POST['tag'] : '';
        if (is_array($tag)) {
            $data['tag'] = implode(',', $tag);
        }
        $validate = new Validate([
            'name' => 'require',
            'gid' => 'require',
            'major_type' => 'require',
            'type' => 'require',
            'des' => 'require',
        ]);
        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        $res = GameModel::update(['id' => $id], $data);
        $output = $res ? ['code' => 1, 'msg' => '更新成功'] : ['code' => 0, 'msg' => '更新失败,请重试'];
        return json($output);
    }

    /**
     * 游戏分类界面
     * @param Request $request
     * @return mixed
     */
    public function typeList(Request $request)
    {
        $where = [];
        $pageSize = $request->get('pageSize', 10);
        $list = GameTypeModel::pagination($where, $pageSize);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch('type');
    }

    /**
     * 添加分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function typeAdd(Request $request)
    {
        $data = [];
        $data['name'] = trim($request->post('type', ''));
        $data['add_time'] = time();
        $data['add_user'] = session('manage_user');
        $isExist = GameTypeModel::one(['name' => $data['name']]);
        if ($isExist) {
            return json(['code' => 0, 'msg' => '分类已存在,请重试添加']);
        }
        $res = GameTypeModel::insert($data);
        $output = $res ? ['code' => 1, 'msg' => '添加成功'] : ['code' => 0, 'msg' => '添加失败,请重试'];
        return json($output);
    }

    /**
     * 游戏标签
     * @param Request $request
     * @return mixed
     */
    public function tagList(Request $request)
    {
        $where = [];
        $pageSize = $request->get('pageSize', 10);
        $list = GameTagModel::pagination($where, $pageSize);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch('tag');
    }

    /**
     * 添加标签
     * @param Request $request
     * @return \think\response\Json
     */
    public function tagAdd(Request $request)
    {
        $tag = $request->post('tag', '');
        $tag = trim($tag);
        if (!$tag) {
            return json(['code' => 0, 'msg' => '标签不能为空']);
        }
        $data = [];
        $data['add_user'] = session('manage_user');
        $data['add_time'] = time();
        $data['name'] = $tag;
        $res = GameTagModel::insert($data);
        $output = $res ? ['code' => 1, 'msg' => '添加成功'] : ['code' => 0, 'msg' => '添加失败,请重试'];
        return json($output);
    }
}